package com.uliky.shouyiLibrary.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;


/**
 * 解决跨域问题
 * 设置拦截器，统一加上响应头
 */
@Configuration
public class ShouyiLibraryCorsConfiguration {
    @Bean
    public CorsWebFilter corsWebFilter(){
        //org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource
        //UrlBasedCorsConfigurationSource是CorsConfigurationSource的实现类
        UrlBasedCorsConfigurationSource sourse = new UrlBasedCorsConfigurationSource();
        //添加跨域配置，
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*"); //请求头
        corsConfiguration.addAllowedMethod("*");//请求方法
        //corsConfiguration.addAllowedOrigin("*");//请求来源
        corsConfiguration.addAllowedOriginPattern("*");
        corsConfiguration.setAllowCredentials(true);//允许携带cookies
        //对哪些路径进行处理,怎么处理
        sourse.registerCorsConfiguration("/**",corsConfiguration);

        return new CorsWebFilter(sourse);
    }
}
